<template>
    <div style="height: 95%;width: 100%;display:flex;justify-content:center;padding-top: 20px">
        <!--弹框DIV 设置弹框的大小的地方-->
        <div style="background: #fff;width: 700px;height: 600px;box-shadow: #D3DCE6 0px 0px 20px;">
            <div style="height:520px;border: #1C84C6;margin-top: 25px" class="scrollbar">
                <div style="height: 30px;">
                    <!--标题-->
                    <span style="font-size: 18px;margin-left: 25px;">
                        新建合同
                    </span>
                    <!--返回列表-->
                    <span @click="tzdz" style="margin-left: 530px;" >
                        <i slot="suffix" style="font-size: 30px;color: #D3DCE6" class="el-icon-close"></i>
                    </span>
                </div>
                <!--height:91%;表单在弹框显示多长-->
                <el-scrollbar style="height:91%;padding-top: 20px;">
                    <div class="shugan"></div>
                    <span style="margin-left: 10px;font-size: 14px;">基本信息</span>
                    <br/>
                    <br/>
                    <!--表单-->
                    <el-form style="margin-top:5px;margin-left: 40px;" :rules="rules" size="mini" :model="htlist" ref="htlist">
                        <el-row>
                            <el-col :span="11">
                                <el-form-item style="width:250px;">
                                    <label slot="label" style="font-size: 13px">主题：</label><br/>
                                    <el-input v-model="htlist.conTheme"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px">
                                    <label slot="label" style="font-size: 13px">编号：</label><br/>
                                    <el-input v-model="htlist.conNo" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item style="width:250px;" prop="clientId">
                                    <label slot="label" style="font-size: 13px">客户名称：</label><br/>
                                    <!--客户名称-->
                                    <el-popover
                                        style="width: 700px; height: 450px;"
                                        placement="right"
                                        trigger="click"
                                        v-model="kehu">
                                    <span style="font-size: 16px;">关联客户模块</span>
                                    <br/>
                                        <el-input @keyup.enter.native="cx()" v-model="kehuinput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx()"></i>
                                        </el-input>
                                    <el-button type="primary" style="margin-left: 130px;">新 建</el-button>
                                    <el-table
                                            highlight-current-row
                                            border
                                            ref="kehulist"
                                            :data="kehulist">
                                        <el-table-column type="selection" width="50" align="center" property="clientId"></el-table-column>
                                        <el-table-column width="150" property="clientName" label="客户名称"></el-table-column>
                                        <el-table-column width="100" property="clientRank" label="级别"></el-table-column>
                                        <el-table-column width="150" property="clientMobile" label="手机号码"></el-table-column>
                                        <el-table-column width="200" property="clientSite" label="地址"></el-table-column>
                                    </el-table>
                                    <el-pagination
                                            style="margin-left: 350px;"
                                            layout="prev, pager, next"
                                            @current-change="handleCurrentChange"
                                            :page-size="pageSize"
                                            :current-page.sync="currPage"
                                            :total="totalSize">
                                    </el-pagination>
                                    <div style="text-align: center;width: 100%" class="dialog-footer">
                                        <el-button type="primary" @click="xzkehu">保 存</el-button>
                                        <el-button type="primary" @click="kehu = false">取 消</el-button>
                                    </div>
                                    <el-input v-if="this.xg==0" v-model="htlist.clientId.clientName" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                </el-popover>
                                    <el-input v-if="this.xg==1" :disabled="true" v-model="htlist.clientId.clientName" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="busId">
                                    <label slot="label" style="font-size: 13px">商机名称：</label><br/>
                                    <!--商机-->
                                    <el-popover
                                            style="width: 700px; height: 450px;"
                                            placement="right"
                                            trigger="click"
                                            v-model="shangji">
                                        <span style="font-size: 16px;">关联商机模块</span>
                                        <br/>
                                        <el-input @keyup.enter.native="cx2()" v-model="shangjiinput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx2()"></i>
                                        </el-input>
                                        <el-button type="primary" style="margin-left: 100px;">新 建</el-button>
                                        <el-table
                                                highlight-current-row
                                                border
                                                ref="shangjilist"
                                                :data="shangjilist">
                                            <el-table-column type="selection" width="50" align="center" property="busId"></el-table-column>
                                            <el-table-column width="160" property="busNo" label="商机编号"></el-table-column>
                                            <el-table-column width="150" property="busName" label="主题"></el-table-column>
                                            <el-table-column label="建立时间" align="center" width="200">
                                                <template slot-scope="d">
                                                    {{d.row.busSettime | formatDate}}
                                                </template>
                                            </el-table-column>
                                            <el-table-column width="100" property="busMoney" label="总金额"></el-table-column>
                                        </el-table>
                                        <el-pagination
                                                style="margin-left: 350px;"
                                                layout="prev, pager, next"
                                                @current-change="handleCurrentChange2"
                                                :page-size="pageSize2"
                                                :current-page.sync="currPage2"
                                                :total="totalSize2">
                                        </el-pagination>
                                        <div style="text-align: center;width: 100%" class="dialog-footer">
                                            <el-button type="primary" @click="xzdd">保 存</el-button>
                                            <el-button type="primary" @click="dindan = false">取 消</el-button>
                                        </div>
                                        <el-input  v-if="this.xg==0" v-model="htlist.busId.busName" @focus="dakai" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                    </el-popover>
                                    <el-input v-if="this.xg==1" :disabled="true" v-model="htlist.busId.busName" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item style="width:250px;">
                                    <label slot="label" style="font-size: 13px">总金额：</label><br/>
                                    <el-input v-model="htlist.conTotal" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px;">
                                    <label slot="label" style="font-size: 13px">毛利润：</label><br/>
                                    <el-input v-model="htlist.conGross" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item style="width:250px;" prop="conSignatory">
                                    <label slot="label" style="font-size: 13px">客户签约人：</label><br/>
                                    <!--客户签约人-->
                                    <el-input v-model="htlist.conSignatory"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="mySignatory">
                                    <label slot="label" style="font-size: 13px">我方签约人：</label><br/>
                                    <el-input v-model="htlist.mySignatory"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item style="width:250px;" prop="conTime">
                                    <label slot="label" style="font-size: 13px">签约时间：</label><br/>
                                    <el-date-picker
                                            v-if="this.xg==0"
                                            v-model="htlist.conTime"
                                            type="datetime"
                                            placeholder="选择日期时间"
                                            align="right"
                                            :picker-options="pickerOptions"
                                            style="width:250px">
                                    </el-date-picker>
                                    <el-date-picker
                                            v-if="this.xg==1"
                                            :disabled="true"
                                            v-model="htlist.conTime"
                                            type="datetime"
                                            placeholder="选择日期时间"
                                            align="right"
                                            :picker-options="pickerOptions"
                                            style="width:250px">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8" >
                                <el-form-item style="width:250px;" prop="conDeadline">
                                    <label slot="label" style="font-size: 13px">有效期：</label><br/>
                                    <el-date-picker
                                            v-model="htlist.conDeadline"
                                            type="datetime"
                                            placeholder="选择日期时间"
                                            align="right"
                                            :picker-options="pickerOptions2"
                                            style="width:250px">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item style="width:250px;">
                                    <label slot="label" style="font-size: 13px">负责人：</label><br/>
                                    <el-input v-model="htlist.conPeople" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="19">
                                <el-form-item>
                                    <label slot="label" style="font-size: 13px">备注：</label><br/>
                                    <textarea style="width: 610px;" v-model="htlist.conRemark" placeholder="备注说明"></textarea>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <span style="margin-left:30px;font-size: 13px">产品</span>
                    <br/>
                    <br/>
                    <template>
                        <el-table
                                :data="this.htlist.zcontractdetailsByConId"
                                style="width: 76%;margin-left: 30px;font-size:13px">
                            <el-table-column
                                    label="产品名称"
                                    align="center"
                                    prop="prodetailId.productName">
                            </el-table-column>
                            <el-table-column
                                    label="规格"
                                    align="center"
                                    width="150">
                                <template slot-scope="s">
                                    <span v-for="pro in s.row.prodetailId.qmodeldetails" :key="pro.prodetailId">
                                        {{pro.modetContent}}
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    prop="condeJg"
                                    label="售价"
                                    align="center">
                            </el-table-column>
                            <el-table-column
                                    label="数量"
                                    align="center"
                                    prop="condeNum">
                            </el-table-column>
                            <el-table-column
                                    label="折扣"
                                    align="center">
                                <template slot-scope="s">
                                    <span>{{s.row.condeDiscount}}%</span>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    prop="condeTital"
                                    label="小计"
                                    align="center"
                                    sortable>
                            </el-table-column>
                            <el-table-column
                                    prop="offerdetailTotal"
                                    label="备注"
                                    align="center"
                                    sortable>
                            </el-table-column>
                        </el-table>
                    </template>
                    <br/>
                    <span style="margin-left:500px;font-size: 12px">总金额：</span>
                    <span style="margin-left:10px;font-size: 12px">{{this.htlist.conTotal}}元</span>
                    <br/>
                    <br/>
                </el-scrollbar>
                <!--确定按钮 设置了固定定位-->
                <div style="text-align: center;width: 80%;position: absolute;top:635px;left:230px;" class="dialog-footer">
                    <!--返回列表-->
                    <el-button @click="tzdz">取 消</el-button>
                    <el-button type="primary" @click="addbusiness('htlist')">确 定</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {formatTimeToStr} from '../router//format.js'
    export default {
        name: "ZxinzenHeton",
        data() {
            var hueikuan = (rule, value, callback) => {
                if (this.htlist.clientId.clientName === null || this.htlist.clientId.clientName === '') {
                    callback(new Error('请先选择客户'));
                } else {
                    if (this.htlist.busId.busName === '' || this.htlist.busId.busName === null) {
                        callback(new Error('请选择计划'));
                    }else {
                        callback()
                    }
                }
            };
            var kehua = (rule, value, callback) => {
                if (value.clientName === null || value.clientName === '') {
                    callback(new Error('请选择客户'));
                }else {
                    callback()
                }
            };
            return {
                rules: {
                    clientId: [
                        {validator: kehua, required: true, trigger: ['blur','change']},
                    ],
                    busId: [
                        {validator: hueikuan, required: true, trigger: ['blur','change']},
                    ],
                    mySignatory: [
                        {required: true, message: '请输入我方签约人', trigger: 'change' },
                    ],
                    conSignatory: [
                        {required: true, message: '请输入客户签约人' ,trigger: 'change' },
                    ],
                    conTime: [
                        {required: true, message: '请选择签约时间', trigger: 'change' },
                    ],
                    conDeadline: [
                        {required: true, message: '请选择合同的有效期', trigger: 'change' },
                    ],
                },
                pickerOptions: {//开票日期快捷选择方式
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    },
                    shortcuts: [
                        {
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                pickerOptions2: {//开票日期快捷选择方式
                    disabledDate(time) {
                        return time.getTime() < Date.now();
                    },
                    shortcuts: [
                        {
                            text: '今天',
                            onClick(picker) {
                                picker.$emit('pick', new Date());
                            }
                        },{
                            text: '一周后',
                            onClick(picker) {
                                const date = new Date();
                                date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
                                picker.$emit('pick', date);
                            }
                        }]
                },
                xg:0,
                kehu:false,//客户弹出框
                kehuinput:'',//客户弹框搜索框
                kehulist:[],//客户表格信息
                pageSize:5,//客户分页页大小
                currPage:1,//客户当前页码，默认为1
                totalSize:0,//客户总记录数
                htlist:{
                    conId:0,
                    busId:{
                        busId:0,
                        client:null,
                        busName:null,
                        busPossibility:null,
                        busWritten:null,
                        busMoney:null,
                        busSettime:null,
                        busNo:null,
                        busPeople:null,
                        busPrimarycontact:null,
                        phase:null,
                        busStatu:null,
                        busUptime:null,
                        dbuspros:null
                    },
                    clientId:{
                        clientId:0,
                        updateTime:null,
                        clientIndustry:null,
                        clientSource:null,
                        clientUrl:null,
                        establishPerson:null,
                        establishTime:null,
                        clientName:null,
                        clientRank:null,
                        clientPhone:null,
                        clientMobile:null,
                        clientSite:null,
                        clientRemark:null,
                        clientPeople:null,
                        clientCjpeople:null,
                        clientState:0,
                        clientIffollowup:0,
                        clientSheng:null,
                        clientSz:null,
                        empId:null,
                        clientPayattention:null,
                        operationId:null,
                        relationwarnId:null,
                        tfylinkmanFormsByClientId:null,
                        zbartersByClientId:null,
                        zcontractsByClientId:null,
                        zdeliverylogsByClientId:null,
                        zinvoicesByClientId:null,
                        zordersByClientId:null
                    },
                    conNo:null,
                    conTheme:null,
                    conTotal:0,
                    conSignatory:null,
                    mySignatory:null,
                    conTime:null,
                    conDeadline:null,
                    conAccessory:null,
                    conRemark:null,
                    conPeople:null,
                    conZt:0,
                    conGross:0,
                    zcontractdetailsByConId:[],
                },//合同集合
                shangji:false,//订单弹出框
                shangjiinput:'',//订单弹框搜索框
                shangjilist:[],//订单表格信息
                pageSize2:5,//订单分页页大小
                currPage2:1,//订单当前页码，默认为1
                totalSize2:0,//订单总记录数
                htxq:{//合同详情
                    condeId:0,
                    conId:{
                        conId:0,
                        conNo:null,
                        conTheme:null,
                        conTotal:0,
                        conStarttime:null,
                        conFinishtime:null,
                        conSignatory:null,
                        mySignatory:null,
                        conTime:null,
                        conState:null,
                        conAccessory:null,
                        conRemark:null,
                        conPeople:null,
                        conZt:0,
                        conGross:0,
                    },
                    prodetailId:{
                        prodetailId:0,
                        productId:null,
                        gdId:null,
                        prodetaiJpri:null,
                        productName:null,
                        prodetailSpri:null,
                        prodetailKcnum:null,
                        zbarterdetailsByProdetailId:null,
                        zcontractdetailsByProdetailId:null,
                        zdeliverydeliveriesByProdetailId:null,
                        zdeliverylogdeliveriesByProdetailId:null,
                        zorderdetailsByProdetailId:null,
                        qmodeldetails:null,
                        qpictures:null
                    },
                    condeNum:null,
                    condeTital:null,
                    condeDiscount:null,
                    condeNote:null,
                    condeJg:null
                },
                ys:1,
                ydx:10,
                dz:''
            };
        },
        methods:{
            //确定按钮
            addbusiness(formName) {
                if(this.xg!=0){
                    this.xzfapiao();
                }else{
                    this.$refs[formName].validate(valid => {
                        this.print(valid);
                        window.console.log(valid)
                        if (valid) {
                            this.xzfapiao();
                        }else{
                            this.$message({
                                message: '验证不通过！',
                                type: 'warning'
                            });
                            return false;
                        }

                    });
                }
            },
            //地址
            tzdz(){
                if(this.dz==='htzy'){
                    this.$router.push({
                        path:'/heton',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }
            },
            /*初始化商机编号*/
            initbusNo() {
                var date = new Date();
                var year = date.getFullYear();
                var month = date.getMonth() + 1;
                var day = date.getDate();
                if (month < 10) {
                    month = "0" + month;
                }
                if (day < 10) {
                    day = "0" + day;
                }
                //使用年月日作为中间数
                var busnosj = year + month + day;

                //使用时间戳作为最后五位随机数
                var a = date.getTime() + "";
                var b = a.substring(8, 13);

                //最后编号
                var busno = "HT_" + busnosj + "_" + b;
                this.htlist.conNo = busno;
            },
            /*当前页方法*/
            handleCurrentChange(val) {
                this.initData(val);
            },
            /*当前页方法*/
            handleCurrentChange2(val) {
                this.jihuainitData(val);
            },
            cx(){
                this.initData(this.currPage);
            },
            cx2(){
                this.jihuainitData(this.currPage2)
            },
            // 根据合同id查询对应的通过的合同详情
            htxqinitData(zhi){
                this.$axios.post("http://localhost:8088/heton/list_htxqcp",this.qs.stringify({search:zhi}))
                    .then(v=>{
                            this.htlist.zcontractdetailsByConId=v.data;
                    })
                    .catch()
            },
            // 新增修改
            xzfapiao(){
                this.$axios.post("http://localhost:8088/heton/add_heton",this.htlist)
                    .then(()=>{
                        if(this.xg==0){
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '新增成功!',
                            });

                        }else {
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '修改成功!',
                            });
                        }
                        this.tzdz();
                    }).catch(()=>{
                        if(this.xg==0){
                            this.$message({
                                showClose: true,
                                message: '新增失败',
                                type: 'error'
                            });
                        }else {

                            this.$message({
                                showClose: true,
                                message: '修改失败',
                                type: 'error'
                            });
                        }
                    }
                )
            },
            // 根据商机id查询对应的通过的报价详情
           bjxqinitData(zhi){
               this.$axios.post("http://localhost:8088/heton/bj_xq",this.qs.stringify({sjid:zhi}))
                   .then(v=>{
                        v.data.forEach((a,i)=>{
                            const htxq = {};
                            htxq.prodetailId=null;
                            htxq.condeNum=null;
                            htxq.condeTital=null;
                            htxq.condeDiscount=null;
                            htxq.condeNote=null;
                            htxq.condeJg=null;
                            this.$set(htxq,'prodetailId',a.prodetailId);
                            this.$set(htxq,'condeNum',a.offerdetailNum);
                            this.$set(htxq,'condeTital',a.offerdetailTotal);
                            this.$set(htxq,'condeDiscount',a.offerdetailDeiscount);
                            this.$set(htxq,'condeNote',a.offerdetailNote);
                            this.$set(htxq,'condeJg',this.numFilter(a.offerdetailTotal/a.offerdetailNum));
                            this.htlist.conTotal=a.offerId.offerMoney;
                            this.htlist.conGross=a.offerId.offerProfit;
                            this.htlist.zcontractdetailsByConId.splice(i,0,htxq);
                        })
                   })
                   .catch()
            },
            //点击商机输入框就查询
            dakai() {
                this.jihuainitData(this.currPage2);
            },
            //选择商机确定按钮
            xzdd() {
                this.$refs.shangjilist.selection.forEach((pro)=>{
                    this.htlist.busId=pro;
                    this.htlist.zcontractdetailsByConId=[];
                });
                this.bjxqinitData(this.htlist.busId.busId);
                this.shangji=false;
                this.shangjiinput='';
            },
            //商机查询方法
            jihuainitData(dd) {
                this.$axios.post("http://localhost:8088/business/kehushangji",this.qs.stringify(
                    {search:this.shangjiinput,search2:this.htlist.clientId.clientId, page:dd, size:this.pageSize2,}))
                    .then(v=>{
                        this.shangjilist = v.data.rows;
                        this.totalSize2 = v.data.total;
                    })
                    .catch()
            },
            //选择客户确定按钮
            xzkehu() {
                this.$refs.kehulist.selection.forEach((pro) => {
                    this.htlist.clientId = pro;
                });
                this.kehu = false;
                this.kehuinput = '';
            },
            //客户查询方法
            initData(dd) {
                this.$axios.post("http://localhost:8088/dindan/kehu_list", this.qs.stringify(
                    {search: this.kehuinput, page: dd, size: this.pageSize,}))
                    .then(v => {
                        this.kehulist = v.data.rows;
                        this.totalSize = v.data.total;
                    })
                    .catch()
            },
            /*去掉小数点*/
            numFilter (value) {
                let realVal = ''
                if (!isNaN(value) && value!== '') {
                    // 截取当前数据到小数点后两位
                    realVal = parseFloat(value).toFixed(2)
                } else {
                    realVal = '--'
                }
                return realVal
            }
        },
        created: function () {
            this.htlist.conPeople = JSON.parse(localStorage.getItem("user")).empName;
            if(this.$route.query.key!=null){
                this.htlist=this.$route.query.key;
                this.xg = 1;
                this.htxqinitData(this.htlist.conId)
            }else{
                this.initData(this.currPage);
                this.initbusNo();
            }
            if(this.$route.query.ys!=null){
                this.ys=this.$route.query.ys;
            }
            if(this.$route.query.ydx!=null){
                this.ydx=this.$route.query.ydx;
            }
            if(this.$route.query.dz!=null){
                this.dz=this.$route.query.dz;
            }
        },
        filters: {
            formatDate: function (time) {
                if (time != null && time != "") {
                    var date = new Date(time);
                    return formatTimeToStr(date, "yyyy-MM-dd hh:mm:ss");
                } else {
                    return "";
                }
            }
        }
    }
</script>

<style scoped>
    .shugan{
        border-left-color: rgb(70, 205, 207);
        border-left-width: 1.8px;
        height: 10px;
        border-left-style: solid;
        display: inline;
        font-size: 13px;
        margin-left: 25px;
    }

    /deep/ .el-scrollbar__wrap {
        overflow: scroll;
        width: 120%;
        height: 100%;
    }
</style>